package com.huangliusong.threadcurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author huangliusong
 * @since 2018/11/21
 * {@link }
 */
@Slf4j
public class CountDownLatchExample {
    private static int threadCount=200;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService=Executors.newCachedThreadPool();
        final CountDownLatch countDownLatch=new CountDownLatch(threadCount);
        for (int i=0;i<threadCount;i++){
            final int threadNum=i;
            executorService.execute(()->{
                try {
                    test(threadNum);
                }catch (Exception e){
                    e.printStackTrace();
                    log.error("{}",e);
                }
            });
        }
        countDownLatch.await();
        log.info("finish");
        executorService.shutdown();
    }


    private static void test(int threadNum) throws  Exception{
        Thread.sleep(100);
        log.info("{}",threadNum);
        Thread.sleep(100);
    }
}
